package far;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import commans.ErrorPage;

import entity.FirePolicyEntity;

import main.CreatePolicyMain;

//住火套表
public class FirePolicyATypeBlank extends CreatePolicyMain 
{
    private List<FirePolicyEntity> ply;
    private String plyType = null;

    public FirePolicyATypeBlank(List<FirePolicyEntity> policys)
    {
        setVisible(true);
        this.ply = policys;
        addWindowListener(new WindowAdapter()
        {
	  public void windowClosing(WindowEvent e)
	  {
	      Map liveThreads = Thread.getAllStackTraces();
	      for(java.util.Iterator i = liveThreads.keySet().iterator(); i.hasNext();)
	      {
		Thread key = (Thread)i.next();
		if(key.getName().equals("CreatePolicy"))
		{
		    key.stop();
		    // System.out.println("Thread "+key.getName());
		    document.close();
		    deleteFile(path);
		}
	      }
	  }
        });
    }

    public void CreatePolicy()
    {

        // Count Total
        for(FirePolicyEntity ply : this.ply)
        {
	  if(ply.getNEWPLY().substring(4,5).equals("A"))
	      count = count+1;
        }
        totalBytes = count;
        try
        {
	  path = "C:\\A.pdf";
	  document = new Document(PageSize.A4,72,36,173,-60);
	  // 建檔
	  // 產出檔
	  PdfWriter.getInstance(document,new FileOutputStream(path));

	  // 設定字型
	  BaseFont bfChinese = BaseFont.createFont("C:\\windows\\fonts\\MINGLIU.TTC,1",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
	  Font FontChinese = new Font(bfChinese,10,Font.NORMAL);
	  Font FontChinese1 = new Font(bfChinese,7,Font.NORMAL);
	  Font FontChinese2 = new Font(bfChinese,8,Font.NORMAL);
	  document.addTitle("POLICY");
	  document.addAuthor("MINGTAI");
	  document.addSubject("MINGTAI");
	  document.addKeywords("MINGTAI");
	  document.addCreator("MINGTAI");
	  document.open();
	  // 選擇檔案
	  for(FirePolicyEntity ply : this.ply)
	  {
	      if(ply.getNEWPLY().substring(4,5).equals("A"))
	      {
		commit = commit+1;
		// 寫入PDF
		// 保單號
		// 保單號
		// 保單號
		document.add(new Paragraph(-4,"                        "+ply.getNEWPLY().substring(0,2)+"         "+ply.getNEWPLY().substring(2,10)+"                                       "+ply.getPLYNO()+"                                  ",FontChinese));
		// 被保人
		document.add(new Paragraph(21,"                   "+ply.getFASRCNAME()+"                                                                                             "+ply.getAREANO()+ply.getADDR().substring(0,15)+"                     ",FontChinese2));
		document.add(new Paragraph(-12,"                                                                                                                                                                          "+ply.getADDR().substring(15),FontChinese2));
		document.add(new Paragraph(12,"                                               "+ply.getFASRIDNO()+"                   "+ply.getTEL(),FontChinese));
		// 要保人
		document.add(new Paragraph(14,"                   "+ply.getINSDNAME()+"                                                                                             "+ply.getAREANO()+ply.getADDR().substring(0,15)+"                     ",FontChinese2));
		document.add(new Paragraph(-5,"                                                                                                                                                                          "+ply.getADDR().substring(15),FontChinese2));
		document.add(new Paragraph(12,"                                              "+ply.getIDNO()+"                   "+ply.getTEL(),FontChinese));
		// 總保額
		num1 = 0;
		try
		{
		    if(ply.getFIREPREM().trim()!=null)
		    {
		        num1 = num1+Integer.parseInt(ply.getFIREPREM().trim());
		    }
		}
		catch (NumberFormatException e2)
		{
		    num1 = 0+num1;
		}
		try
		{
		    if(ply.getEPREM().trim()!=null)
		    {
		        num1 = num1+Integer.parseInt(ply.getEPREM().trim());
		    }
		}
		catch (NumberFormatException e2)
		{
		    num1 = 0+num1;
		}
		try
		{
		    if(ply.getPREM2().trim()!=null)
		    {
		        num1 = num1+Integer.parseInt(ply.getPREM2().trim());
		    }
		}
		catch (NumberFormatException e2)
		{
		    num1 = 0+num1;
		}
		String money = function.CatZero(num1);
		document.add(new Paragraph(16,"                               "+function.CatZero(ply.getINSDAMT()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                              "+function.CatZero(ply.getFIREPREM()),FontChinese));
		document.add(new Paragraph(13,"                                                                                                      "+money,FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                               "+function.CatZero(ply.getEPREM()),FontChinese));
		document.add(new Paragraph(13,"                               "+function.CatZero(ply.getEAMT())+"                                                                                                                     0",FontChinese));

		// 保險期間
		num2 = 1+Integer.parseInt(ply.getEXPDAY().substring(0,3));
		document.add(new Paragraph(26,"                  12                       "+ply.getEXPDAY().substring(0,3)+"         "+ply.getEXPDAY().substring(3,5)+"        "+ply.getEXPDAY().substring(5)+"                                        "+num2+"       "+ply.getEXPDAY().substring(3,5)+"        "+ply.getEXPDAY().substring(5),FontChinese));
		// 標的物地址
		document.add(new Paragraph(27,"                   "+ply.getLOCATION().trim()+ply.getAREA(),FontChinese));
		// 建築物
		for(int i = 0; i<=13; i++)
		{
		    str3[i] = "  ";
		}
		try
		{
		    num1 = Integer.parseInt(ply.getCONSTCLASS());
		}
		catch (NumberFormatException e2)
		{
		    num1 = 0;
		}

		if(num1==1)
		{
		    str4 = "A1";
		}
		else if(num1==2)
		{
		    str4 = "A2";
		}
		else if(num1==3)
		{
		    str4 = "B";
		}
		else
		{
		    str4 = "C";
		}
		// document.add(new Paragraph(" "));
		// 樓層建築等級
		document.add(new Paragraph(10,"             "+str3[0]+"               "+str3[1]+str3[2],FontChinese1));
		document.add(new Paragraph("             "+str3[3]+"               "+str3[4]+str3[5]+str3[6]+str3[7],FontChinese1));
		document.add(new Paragraph("             "+str3[8]+"               "+str3[9]+str3[10]+str3[11]+"                                                                                                                                                                                       "+ply.getFLOOR()+"                              "+str4,FontChinese1));
		// 建築物年份
		document.add(new Paragraph("                                                                         "+ply.getSTRUYEAR()+str3[12]+str3[13],FontChinese1));
		document.add(new Paragraph(0,"                                                                                               "+ply.getCONSTMARK(),FontChinese1));
		// 建築物坪數
		document.add(new Paragraph("                                                                                "+CatZero(ply.getAREASIZE()),FontChinese1));
		document.add(new Paragraph(92,"   ",FontChinese));
		// 分項保費建築物
		document.add(new Paragraph(20,"    01                                                                                "+function.CatZero(ply.getINSDAMT())+"         "+ply.getRATE().substring(1)+"    1/1        "+function.CatZero(ply.getFIREPREM()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                                                       "+str4,FontChinese));
		// 加保動產
		document.add(new Paragraph(12,"  ",FontChinese));
		document.add(new Paragraph(12,"  ",FontChinese));
		document.add(new Paragraph(12,"          自101年1月1日起，住宅地震基本保險最高賠償責任調整為新台幣150萬元。",FontChinese));
		// 基本地震
		document.add(new Paragraph(18,"    02                                                                                "+function.CatZero(ply.getEAMT())+"         "+"0.9        "+"1/1        "+function.CatZero(ply.getEPREM()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                                                       "+str4,FontChinese));
		checkAMT = Integer.parseInt(ply.getAMT2().trim());
		if(checkAMT>0)
		{
		    document.add(new Paragraph(20,"    03    建築物",FontChinese));
		    document.add(new Paragraph(0,"                                                                                        "+function.CatZero(ply.getAMT2())+"           "+ply.getRATE2()+"      "+"1/1        "+function.CatZero(ply.getPREM2()),FontChinese));
		    document.add(new Paragraph(0,"                                                                                                                                                                                       "+str4,FontChinese));
		}
		else
		{
		    document.add(new Paragraph(" "));
		}
		// 備註
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		// 貸款編號
		document.add(new Paragraph(-8,"                                                                                                              "+ply.getBANKKEY(),FontChinese));
		// 抵押權人/分行別
		document.add(new Paragraph(16,"                                                                                                     "+ply.getMTG()+"                                        "+ply.getAGENTUNIT(),FontChinese));
		// 行員代號
		document.add(new Paragraph(16,"                                                                                                                                                         "+ply.getAGENTEMPNO(),FontChinese));
		// 其他
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));

		document.add(new Paragraph(70,"            "+ply.getAGENT()+"               "+ply.getAGENTUNIT()+"                   "+ply.getBROKER()+"                   "+ply.getEMPA()+"                   "+ply.getEMPA1()+"                          "+ply.getSTUNIT()+"                          "+ply.getSEMP1(),FontChinese));
		document.add(new Paragraph(14,"16.9% + 6%",FontChinese));
		document.newPage();
		// ProgressBar Mode
		byteRead = byteRead+1;
		currentValue = (int) ( byteRead*100/totalBytes );
		progressBar.setValue(currentValue);
		broker = ply.getBROKER();
	      }
	  }
	  document.close();
	  path = function.ChangeBankName(broker,path,"基本住宅","套表列印");
	  JOptionPane.showMessageDialog(null,path);
	  dispose();

        }
        catch (Exception e1)
        {
	  document.close();
	  System.gc();
	  deleteFile(path);
	  new ErrorPage(e1);

        }
        finally
        {
	  document.close();
	  System.gc();
        }
    }

    @Override
    public void run()
    {
        CreatePolicy();
    }

}
